import express from 'express';
import { teachingAnalysisController } from '../controllers/teachingAnalysisController';
import { roleAuth } from '../middleware/roleAuth';
import { Role } from '@prisma/client';
import { validateRequest } from '../middleware/validation';
import { z } from 'zod';

const router = express.Router();

// 验证Schema
const teacherParamSchema = z.object({
  params: z.object({
    teacherId: z.string().uuid('无效的教师ID')
  })
});

const dateQuerySchema = z.object({
  query: z.object({
    startDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, '开始日期格式应为YYYY-MM-DD'),
    endDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, '结束日期格式应为YYYY-MM-DD')
  }).refine(
    data => new Date(data.startDate) <= new Date(data.endDate),
    { message: '开始日期不能晚于结束日期' }
  )
});

// 合并schema
const analysisSchema = teacherParamSchema.merge(dateQuerySchema);

// 路由配置
router.get(
  '/:teacherId/evaluation',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(teacherParamSchema),
  teachingAnalysisController.getTeacherEvaluation
);

router.get(
  '/:teacherId/recommendations',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(teacherParamSchema),
  teachingAnalysisController.getTeachingRecommendations
);

router.get(
  '/:teacherId/period-analysis',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(analysisSchema),
  teachingAnalysisController.getTeacherAnalysisByPeriod
);

export default router;
